package com.sca4cloud.sca.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

//  /ws/net/info
@Slf4j
@RestController
public class WebSocketController {
	@GetMapping("/net/info")
	public void wntInfo(HttpServletRequest req, HttpServletResponse response) {
		// 允许通过该请求
		String origin = req.getHeader(HttpHeaders.ORIGIN);
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, Boolean.TRUE.toString());
		response.setHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);
		response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);
		try {
			response.getWriter().write("true");
		} catch (IOException e) {
			log.error("{}", e);
		}
	}
}
